import 'dart:io';
import 'package:dio/dio.dart';
import 'package:xv_flutter/config/config.dart';

// 通用http请求
Future request(url, {formData}) async {
  print('开始获取数据............');

  try {
    Response response;
    Dio dio = Dio();
    dio.options.contentType = ContentType.parse('application/x-www-form-urlencoded').toString();
    if (formData == null) {
      response = await dio.post(servicePath[url]);
    } else {
      response = await dio.post(servicePath[url], data: formData);
    }

    // statusCode为 2XX,304
    if (response.statusCode == 200) {
      return response.data;
    } else {
      throw Exception('后端接口出现问题');
    }
  } on DioError catch(e) {
    return print('ERROR:===> $e');
  }
}
